﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using eCommerce.DataAccess;

namespace eCommerce.Site
{
    public partial class Basket : System.Web.UI.Page
    {
        public string StrCart = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            Bind();

        }
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            Bind();
        }

        /// <summary>
        /// Handles the RowUpdating event of the GridView1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewUpdateEventArgs"/> instance containing the event data.</param>
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            HiddenField hdf = (HiddenField)row.FindControl("HiddenField1");
            TextBox textqty = (TextBox)row.FindControl("textbox1");

            GridView1.EditIndex = -1;

            using (eCommerceDataContext db = new eCommerceDataContext())
            {
                String tbx = Session["cartid"].ToString();
                Guid g = new Guid(tbx);
                var listz = (from b in db.Cart_Products
                             where b.productid == Convert.ToInt32(hdf.Value) & b.cartid == g
                             select b).FirstOrDefault();
                listz.price = (listz.price/listz.qty)*Convert.ToInt32(textqty.Text);
                listz.qty = Convert.ToInt32(textqty.Text);

                db.SubmitChanges();
                lblsubmit.Text = "Your list has been updated!";
            }


            Bind();

        }


        /// <summary>
        /// Binds this instance.
        /// </summary>
        public void Bind()
        {
            eCommerceDataContext db = new eCommerceDataContext();
            CartDA cartDA = new CartDA();
            String tbx = cartDA.CartID.ToString();
            
            //GetCartID()
            //String tbx = Session["cartid"].ToString();
            Guid g = new Guid(tbx);
            var oCodes = (from p in db.Cart_Products
                          where p.cartid == g
                          select p.productid);
            foreach (var p in oCodes)
            {

                if (p != 0)
                {
                    var newsletter = db.getBasketBySession(tbx);
                    GridView1.DataSource = newsletter;
                    GridView1.DataBind();

                }
                //else
                //{
                //    var newsletter = db.getBasketBySessionNOCode(tbx);
                //    GridView2.DataSource = newsletter;
                //    GridView2.DataBind();
                //}
            }
            decimal amountT = 0;
            var oPrice = (from p in db.Cart_Products
                          where p.cartid == g
                          select p.price);
            foreach (var p in oPrice)
            {
                amountT = amountT + p.Value;
            }
            litTotalAmount.Text = Convert.ToString(amountT);




        }
        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {

        }
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            Bind();

        }
       

      

        protected void ButtonDeleteCommand(object sender, CommandEventArgs e)
        {


            using (eCommerceDataContext ndc = new eCommerceDataContext())
            {

                String tbx = Session["cartid"].ToString();
                Guid g = new Guid(tbx);

                var oCodes = (from p in ndc.Cart_Products
                              where p.productid == Convert.ToInt32(e.CommandArgument) & p.cartid == g
                              select p).First();

                string record = "'" + oCodes.qty + "' has been deleted";

                ndc.Cart_Products.DeleteOnSubmit(oCodes);
                ndc.SubmitChanges();
                lblsubmit.Text = record;
               
                var newsletter = ndc.getBasketBySession(tbx);
                GridView1.DataSource = newsletter;
                GridView1.DataBind();

            }


            Bind();
        }

        protected void BtnProceedClick(object sender, EventArgs e)
        {

            Response.Redirect("Checkout/CustomerDetails.aspx");
           
        }
    }
}
